package MyGui;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.*;
import java.io.File;

import BackGround.*;
import DistSys.*;
import Element.Geom.AbsElemGeom;

/**
 * Title:        iPAD
 * Description:
 * Copyright:    Copyright (c) 2002
 * Company:      ABB Inc.
 * @author Fangxing Li
 * @version 1.0
 */

public class EditBgImageDlg extends MyDlgFrame implements WindowListener, ActionListener{

  private JLabel      lblName, lblImgFile, lblLocX, lblLocY, lblWidth, lblHeight;
  private JTextField  tfName, tfImgFile, tfLocX, tfLocY, tfWidth, tfHeight;
  private JButton     btnChooseFile, btnRefresh, btnZoomAll;

//  private boolean     bIsVerifier=false;
  public EditBgImageDlg(MainFrame parent, String title ){
      super( parent, title );
      init();
  }

  public void init(){
      JPanel pGeneral = this.createGeneralPanel();
      JPanel pLocation = this.createLocationPanel();
      JPanel pSize = this.createSizePanel();
      btnRefresh = new JButton("Refresh Screen");
      btnZoomAll = new JButton("Zoom All");

      Container pane = this.getContentPane();
      pane.setLayout( this.gridBagLayout );
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.insets = new Insets(2,2,2,2);
      gbc.fill = GridBagConstraints.BOTH;
      gbc.weightx = gbc.weighty =1;
      gbc.gridx = 0; gbc.gridy=0; gbc.gridwidth=2; pane.add( pGeneral, gbc);
      gbc.gridx = 0; gbc.gridy=1; gbc.gridwidth=1; pane.add( pLocation, gbc);
      gbc.gridx = 1; gbc.gridy=1; gbc.gridwidth=1; pane.add( pSize, gbc);
      gbc.gridx = 0; gbc.gridy=2; gbc.gridwidth=1; pane.add( btnRefresh, gbc);
      gbc.gridx = 1; gbc.gridy=2; gbc.gridwidth=1; pane.add( btnZoomAll, gbc);

      this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
      this.setLocation(200,200);
      this.setSize(300, 230);

      this.addWindowListener( this );   //windows listener
      btnZoomAll.addActionListener(this);
      btnRefresh.addActionListener(this);
      btnChooseFile.addActionListener(this);

      this.tfName.addFocusListener( focusListener);
      this.tfLocX.addFocusListener( focusListener);
      this.tfLocY.addFocusListener( focusListener);
      this.tfWidth.addFocusListener( focusListener);
      this.tfHeight.addFocusListener( focusListener);
  }

  private JPanel createGeneralPanel(){
      JPanel pGeneral = new JPanel();
      pGeneral.setBorder( new TitledBorder("General"));
      lblName = new JLabel("Name:");
      tfName = new JTextField(15);
      lblImgFile = new JLabel("Image file:");
      tfImgFile = new JTextField(15);
      tfImgFile.setEditable(false);
      btnChooseFile = new JButton("...");

      pGeneral.setLayout( gridBagLayout );
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.fill = GridBagConstraints.BOTH;
      gbc.insets = new Insets(1,1,1,1);
      gbc.weightx=1;
      gbc.weighty=1;
      gbc.gridx = 0; gbc.gridy = 0; pGeneral.add(lblName, gbc);
      gbc.gridx = 1; gbc.gridy = 0; pGeneral.add(tfName, gbc);
      gbc.gridx = 0; gbc.gridy = 1; pGeneral.add(lblImgFile, gbc);
      gbc.gridx = 1; gbc.gridy = 1; pGeneral.add(tfImgFile, gbc);
      gbc.gridx = 2; gbc.gridy = 1; pGeneral.add(btnChooseFile, gbc);
      return pGeneral;
  }//end of createGeneralPanel();

  private JPanel createLocationPanel(){
      JPanel pLocation = new JPanel();
      pLocation.setBorder( new TitledBorder("Location"));
      this.lblLocX = new JLabel("X: ");
      this.tfLocX = new JTextField(8);
      this.lblLocY = new JLabel("Y: ");
      this.tfLocY = new JTextField(8);

      pLocation.setLayout( gridBagLayout );
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.fill = GridBagConstraints.BOTH;
      gbc.weightx=1;
      gbc.weighty=1;
      gbc.gridx = 0; gbc.gridy = 0; pLocation.add(lblLocX, gbc);
      gbc.gridx = 1; gbc.gridy = 0; pLocation.add(tfLocX, gbc);
      gbc.gridx = 0; gbc.gridy = 1; pLocation.add(lblLocY, gbc);
      gbc.gridx = 1; gbc.gridy = 1; pLocation.add(tfLocY, gbc);
      return pLocation;
  }//end of createLocationPanel();

  private JPanel createSizePanel(){
      JPanel pSize = new JPanel();
      pSize.setBorder( new TitledBorder("Size"));
      this.lblWidth = new JLabel("Width: ");
      this.tfWidth = new JTextField(8);
      this.lblHeight = new JLabel("Height: ");
      this.tfHeight = new JTextField(8);

      pSize.setLayout( gridBagLayout );
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.fill = GridBagConstraints.BOTH;
      gbc.weightx = gbc.weighty=1;
      gbc.gridx = 0; gbc.gridy = 0; pSize.add(lblWidth, gbc);
      gbc.gridx = 1; gbc.gridy = 0; pSize.add(tfWidth, gbc);
      gbc.gridx = 0; gbc.gridy = 1; pSize.add(lblHeight, gbc);
      gbc.gridx = 1; gbc.gridy = 1; pSize.add(tfHeight, gbc);
      return pSize;
  }//end of createSizePanel();

  /**
   * If only one background line is selected, use this one to update text.
   * Other wise, update based on default bakcgorund line.
   */
  public void updateInfo() {
      if( Sys.selectedList.size() == 1 ){
          if ( !(Sys.selectedList.get(0) instanceof BgImage )) { return; }

          BgImage bgImage = (BgImage) Sys.selectedList.get(0);
          AbsElemGeom bgImageGeom = bgImage.getElemGeomObj();
          tfName.setText( bgImageGeom.getName());
          tfImgFile.setText( bgImage.getImageSource() );
          tfLocX.setText( ""+bgImageGeom.x1 );
          tfLocY.setText( ""+bgImageGeom.y1 );
          tfWidth.setText( ""+ ( bgImageGeom.x2 - bgImageGeom.x1));
          tfHeight.setText( ""+ (bgImageGeom.y2 - bgImageGeom.y1));
          this.enableAllGuiCmp();
      } else {
          tfName.setText( "" );
          tfImgFile.setText( "" );
          tfLocX.setText( "" );
          tfLocY.setText( "" );
          tfWidth.setText("");
          tfHeight.setText("");
          this.disableAllGuiCmp();
      }
  }

  private void disableAllGuiCmp(){
      tfName.setEnabled(false);
      tfImgFile.setEnabled(false);
      tfLocX.setEnabled(false);
      tfLocY.setEnabled(false);
      tfWidth.setEnabled(false);
      tfHeight.setEnabled(false);
      btnChooseFile.setEnabled(false);
      btnRefresh.setEnabled(false);
      btnZoomAll.setEnabled(false);
  }

  private void enableAllGuiCmp(){
      tfName.setEnabled(true);
      tfImgFile.setEnabled(true);
      tfLocX.setEnabled(true);
      tfLocY.setEnabled(true);
      tfWidth.setEnabled(true);
      tfHeight.setEnabled(true);
      btnChooseFile.setEnabled(true);
      btnRefresh.setEnabled(true);
      btnZoomAll.setEnabled(true);
  }

  /**
   * implement action listener for
   * btnColor, btnRefresh, and btnSetAsDefault
   */
  public void actionPerformed( ActionEvent evt ){
      Object obj = evt.getSource();
      if( obj == this.btnZoomAll ){
          saveToBgImage();
          mainFrame.menuBar.zoomAll();
          mainFrame.statusBar.updateText( );
      } else if( obj == btnRefresh ){
          saveToBgImage();
          mainFrame.mainPanel.needRedraw = true;
          mainFrame.mainPanel.repaint();
          mainFrame.statusBar.updateText( );
      } else if( obj == btnChooseFile ){
          // instantiate a FileChooser box
          JFileChooser fileChooser = mainFrame.menuBar.fileChooser;
          fileChooser.setCurrentDirectory(Sys.personalInfo.getProjectsPath());
          fileChooser.setDialogTitle("Load Background Images");
          fileChooser.resetChoosableFileFilters();
          fileChooser.addChoosableFileFilter(new GifFileFilter());
          fileChooser.addChoosableFileFilter(new JpegFileFilter());
          fileChooser.addChoosableFileFilter(new PngFileFilter());
          fileChooser.addChoosableFileFilter(new AllImageFileFilter());

          // get file name (*.jpg, *.gif)
          int returnVal = fileChooser.showOpenDialog(mainFrame);
          if (returnVal == JFileChooser.APPROVE_OPTION) {
              File f= fileChooser.getSelectedFile();
              if( !f.exists() ) {
                  JOptionPane.showMessageDialog(this, "File does not exist.");
                  return;
              }
              ((BgImage)Sys.selectedList.get(0)).updateImageSource( f.getPath() );
              this.tfImgFile.setText( f.getPath() );
          }
      }
  }

  /**
   * save all users input
   */
   private void saveToBgImage( ){
      if( Sys.selectedList.size() != 1 ) { return; }
      else if ( !(Sys.selectedList.get(0) instanceof BgImage )) { return; }
      try{
          BgImage bgImage = (BgImage) Sys.selectedList.get(0);
          bgImage.getElemGeomObj().setName( tfName.getText());
          bgImage.setImageSource( tfImgFile.getText());
          int x = Integer.parseInt( tfLocX.getText() );
          int y = Integer.parseInt( tfLocY.getText() );
          int width = Integer.parseInt( tfWidth.getText() );
          int height = Integer.parseInt( tfHeight.getText() );
          bgImage.getElemGeomObj().setNode1FromScreen( x, y);
          bgImage.getElemGeomObj().setNode2FromScreen( x+width, y+height);
          bgImage.getElemGeomObj().calcFrame();
      }catch ( NumberFormatException e ){
          //JOptionPane.showMessageDialog( mainFrame, "Not a valide number" );
      }catch( Exception e){
          //JOptionPane.showMessageDialog( mainFrame, e.toString() );
      }
  }//end of setAsDefault


  /**
   * Implement WindowListener interfaces
   */
    public void windowOpened( WindowEvent evt ){ }
    public void windowClosed( WindowEvent evt ){ }
    public void windowIconified( WindowEvent evt ){ }
    public void windowDeiconified( WindowEvent evt ){ }
    public void windowActivated( WindowEvent evt ){
        try { updateInfo(); }
        catch(Exception e)  { JOptionPane.showMessageDialog(this, e); }
    }
    public void windowDeactivated( WindowEvent evt ){ }
    public void windowClosing( WindowEvent evt ){ }

  /*============================================
  FocusListener
      1.focusGained()
      2.focusLost()
  ============================================*/
  private FocusListener focusListener = new FocusListener(){
    public void focusGained(FocusEvent evt){ }
    public void focusLost(FocusEvent evt){
        saveToBgImage();
    }
  };
}